import * as swaggerJSDoc from 'swagger-jsdoc';
import * as fs from 'fs';
import * as glob from 'glob';

let ctrls = glob.sync('./dist/controllers/**/*.js');
let models = glob.sync('./dist/models/**/*.js');
let { version, name, description } = require('../../package.json');

const options = {
    swaggerDefinition: {
        info: {
            title: name,
            version,
            description
        },
        basePath: '/api',
        produces: [
            'application/json',
            'text/html'
        ],
        securityDefinitions: {
            ApiKeyAuth: {
                type: 'apiKey',
                in: 'header',
                name: 'Authorization'
            }
        },
        security: [
            {
                ApiKeyAuth: []
            }
        ],
        parameters: {
            id: {
                name: 'id',
                in: 'path',
                required: true,
                type: 'string'
            },
            ids: {
                name: 'ids',
                in: 'query',
                required: true,
                type: 'array',
                items: {
                    type: 'string'
                },
                collectionFormat: 'multi'
            },
            /* keyword: {
                name: 'keyword',
                in: 'query',
                description: '关键字',
                type: 'string'
            }, */
            page: {
                name: 'page',
                in: 'query',
                description: '页码',
                type: 'integer',
                default: 1
            },
            pageSize: {
                name: 'pageSize',
                in: 'query',
                description: '页大小',
                type: 'integer',
                default: 10
            }
        }
    },
    apis: [...models, ...ctrls]
};

const spec = swaggerJSDoc(options);
fs.writeFile('./dist/spec.json', JSON.stringify(spec, null, '\t'), err => { });
